/*
----------------------------------------------------------------------------
Copyright (c) 2005 - 2008, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above
	  copyright notice, this list of conditions and the
	  following disclaimer.

    * Redistributions in binary form must reproduce the above
	  copyright notice, this list of conditions and the following
	  disclaimer in the documentation and/or other materials
	  provided with the distribution.

    * Neither the copyright holder's name nor the names of its
	  contributors may be used to endorse or promote products
	  derived from this software without specific prior written
	  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*--------------------------- TCircularList.h -----------------------------*/
/*===========================================================================
FILE: TCircularList.h
Author: Wangxing SHI (swxlion)
Version: 0.0.2 
Date: 2008-04-26

File Description:
    C++ template for Circular List.

Dependencies:
    Platform: Platform Independent.

NOTE:
	If using with the Memory Pool, please DO NOT use the fuanction Clear(),
	Insert(T &), Pop****(T &). And before destroy instance of the list, please
	ensure the list is empty, because the instance will call the Clear() function
	in the destructor.

History:
    version        author        date       description
	----------------------------------------------------
	0.0.1          swxlion       08-04-26   Created the first version.
===========================================================================*/
#ifndef _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TCIRCULAR_LIST_H_
#define _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TCIRCULAR_LIST_H_

/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "TBasalDataNode.h"
#include <new>
/*===============================================================================
  CLASS & STRUCTURE DEFINITIONS
=============================================================================== */
namespace ASDL
{
	template < typename T >
	class TCircularList
	{
		TDLinkNode<T>*			pHead;
		int                     iLen;

	public:
		TCircularList(): pHead(0), iLen(0)
		{
		}

		~TCircularList()
		{
			Clear();
		}

		/** Type T maybe overload the assignation operator to ensure the right behavior.
		(Consider that pop contained data require using the assignation operator, the
		assignation operator rather than the copy constructor of type T is used here.
		So, the copy constructor is optional.) */
		inline bool Insert( T & data )
		{
			TDLinkNode<T>* pNewNode = new(std::nothrow) TDLinkNode<T>; // new(std::nothrow) TDLinkNode<T>(data);
			if( !pNewNode )
				return false;
			else
				pNewNode->Data = data;

			if( pHead )
			{
				pNewNode->Pre = pHead->Pre;
				pNewNode->Next = pHead;
				pHead->Pre->Next = pNewNode;
				pHead->Pre = pNewNode;
			}
			else
			{
				pNewNode->Pre = pNewNode;
				pNewNode->Next = pNewNode;
				pHead = pNewNode;
			}	

			//pHead = pNewNode;

			iLen += 1;

			return true;		
		}

		inline bool Insert( TDLinkNode<T>* pNewNode )
		{
			if( !pNewNode )
				return false;

			if( pHead )
			{
				pNewNode->Pre = pHead->Pre;
				pNewNode->Next = pHead;
				pHead->Pre->Next = pNewNode;
				pHead->Pre = pNewNode;
			}
			else
			{
				pNewNode->Pre = pNewNode;
				pNewNode->Next = pNewNode;
				pHead = pNewNode;
			}	

			//pHead = pNewNode;		    

			iLen += 1;

			return true;		
		}

		inline TDLinkNode<T>* PopFirst()
		{
			TDLinkNode<T>*	pPointer = pHead;

			if( pHead )
			{
				pHead->Pre->Next = pHead->Next;
				pHead->Next->Pre = pHead->Pre;
				pHead = pHead->Next;

				pPointer->Next = NULL;
				pPointer->Pre  = NULL;

				iLen -= 1;

				if( iLen == 0 )
					pHead = NULL;
			}

			return pPointer;
		}

		/** Type T maybe overload the assignation operator to ensure the right behavior. */
		inline bool PopFirst( T & data )
		{
			TDLinkNode<T>* volatile	pPointer = pHead;

			if( pHead )
			{
				pHead->Pre->Next = pHead->Next;
				pHead->Next->Pre = pHead->Pre;
				pHead = pHead->Next;

				pPointer->Next = NULL;
				pPointer->Pre  = NULL;

				iLen -= 1;

				if( iLen == 0 )
					pHead = NULL;

				data = pPointer->Data;
				delete pPointer;

				return true;
			}	

			return false;
		}

		inline TDLinkNode<T>* PopLast()
		{
			TDLinkNode<T>*	pPointer = NULL;

			if( pHead )
			{
				pPointer = pHead->Pre;

				pPointer->Pre->Next = pHead;
				pHead->Pre = pPointer->Pre;

				pPointer->Next = NULL;
				pPointer->Pre  = NULL;

				iLen -= 1;

				if( iLen == 0 )
					pHead = NULL;
			}

			return pPointer;
		}

		/** Type T maybe overload the assignation operator to ensure the right behavior. */
		inline bool PopLast( T & data )
		{
			TDLinkNode<T>* volatile	pPointer = NULL;

			if( pHead )
			{
				pPointer = pHead->Pre;

				pPointer->Pre->Next = pHead;
				pHead->Pre = pPointer->Pre;

				pPointer->Next = NULL;
				pPointer->Pre  = NULL;

				iLen -= 1;

				if( iLen == 0 )
					pHead = NULL;

				data = pPointer->Data;
				delete pPointer;
			}		

			return true;
		}

		inline TDLinkNode<T>* First()
		{
			return pHead;
		}

		inline void	Cycle( int time = 1 )
		{
			if( pHead )
			{
				if( time > 0 )
					while( time-- )
						pHead = pHead->Next;
				else if( time < 0 )
					while( time++ )
						pHead = pHead->Pre;
			}
		}

		inline int GetLength()
		{
			return iLen;
		}

		inline bool	IsEmpty()
		{
			if( iLen == 0 )
				return true;
			else
				return false;
		}

		inline void Clear()
		{
			if( !pHead )
				return;
			
			pHead->Pre->Next = NULL;
			pHead->Pre = NULL;

			while( pHead )
				pHead->Delete( pHead );

			iLen = 0;
		}
	};
}

#endif // _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TCIRCULAR_LIST_H_
